package io.awen.modules.sign.controller;

import io.awen.common.utils.PageUtils;
import io.awen.common.utils.R;
import io.awen.modules.sign.entity.SignSinkEntity;
import io.awen.modules.sign.qo.SignSinkQo;
import io.awen.modules.sign.service.SignSinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;


/**
 * 泄水池信息表
 *
 * @author liu.wen
 * @email liu.wen@qq.com
 * @date 2022-08-10 00:47:08
 */
@Api(tags = {"容器"})
@RestController
@RequestMapping("sign/signsink")
public class SignSinkController {
    @Autowired
    private SignSinkService signSinkService;

    /**
     * 泄水池实时值
     */
    @PostMapping("/realList")
    public R realList() {
        Collection<SignSinkEntity> values = signSinkService.getSensorSinkMap().values();
        Integer online = 0;
        long minTime = System.currentTimeMillis() - 3600000;
        for (SignSinkEntity entity : values) {
            if (entity.getDataTime() != null && entity.getDataTime().getTime() > minTime) {
                online++;
            }
        }
        return R.ok().put("list", signSinkService.getSensorSinkMap().values()).put("onlineCount", online);
    }

    /**
     * 更新容器液位值
     *
     * @param sensorId
     * @param value
     */
    @ApiOperation("更新容器液位计的值")
    @PostMapping("/updateSensorValue")
    public R updateSensorValue(String sensorId, Double value, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date dataTime) {
        signSinkService.updateSensorValue(sensorId, value, dataTime);
        return R.ok();
    }

    /**
     * 按条件查询 容器 总数
     *
     * @param qo
     * @return
     */
    @PostMapping("/total")
    public R total(@RequestBody SignSinkQo qo) {
        int total = signSinkService.total(qo);
        return R.ok().put("total", total);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sign:signSink:list")
    public R list(@RequestBody SignSinkQo qo) {
        PageUtils page = signSinkService.queryPage(qo);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sign:signSink:info")
    public R info(@PathVariable("id") Long id) {
        SignSinkEntity signSink = signSinkService.findAllSink(id);

        return R.ok().put("signSink", signSink);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sign:signSink:save")
    public R save(@RequestBody SignSinkEntity signSink) {
        signSinkService.saveAndCache(signSink);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sign:signSink:update")
    public R update(@RequestBody SignSinkEntity signSink) {
        signSinkService.updateAndCache(signSink);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sign:signSink:delete")
    public R delete(@RequestBody Long[] ids) {
        signSinkService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
